package com.ruoyi.system.wnjk.producer;

import com.ruoyi.system.wnjk.config.ReceiveItemMQConfig;
import com.ruoyi.system.wnjk.contents.Contents;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 延时队列，自动收货
 */
@Service
public class ReceiveItemProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void createOrder(String orderId) {
        System.out.println("Creating order: " + orderId);


        // 设置消息头中的延迟时间
        rabbitTemplate.convertAndSend(
            ReceiveItemMQConfig.DELAYED_EXCHANGE_NAME,
                ReceiveItemMQConfig.AUTO_CONFIRM_ROUTING_KEY,
            orderId,
            message -> {
                message.getMessageProperties().setDelay((int) Contents.Shop.ORDER_DELAY_TIME);
                return message;
            }
        );

        System.out.println("Sent delayed message for auto confirmation of order: " + orderId);
    }
}
